package diploma.kisel.modelas.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import org.apache.poi.hwpf.model.Ffn;

import diploma.kisel.modelas.domain.Database;
import diploma.kisel.modelas.domain.DatabaseType;
import diploma.kisel.modelas.validation.Validation;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This window is designed for describe new database and
 * save it into database(global).
 * 
 * @author Nikolay Kisel
 *
 */
public class DatabaseWindow extends JFrame {
	
	private int databaseNumberLength = 10;

	private JPanel contentPane;
	private JTextField textFieldNumber;
	private JTextField textFieldCountTables;
	private JTextField textFieldCountDays;
	private JTextField textFieldDataCapacity;
	private JTextField textFieldArchDataCapacity;
	private JTextField textFieldCost;
	
	private static DatabaseWindow frame; 
	
	private ServerWindow currentServerWindow; 
	private WorkplaceWindow currentWorkplaceWindow;

	
	/**
	 * Create the frame.
	 */
	public DatabaseWindow() {
		setTitle("База данных");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 380, 293);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		JLabel label = new JLabel("Номер:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 10, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, contentPane);
		contentPane.add(label);
		
		textFieldNumber = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldNumber, -3, SpringLayout.NORTH, label);
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldNumber, -10, SpringLayout.EAST, contentPane);
		contentPane.add(textFieldNumber);
		textFieldNumber.setColumns(10);
		
		JLabel label_1 = new JLabel("Кол-во таблиц:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_1, 16, SpringLayout.SOUTH, label);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_1, 0, SpringLayout.WEST, label);
		contentPane.add(label_1);
		
		textFieldCountTables = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldCountTables, -3, SpringLayout.NORTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldCountTables, 0, SpringLayout.EAST, textFieldNumber);
		contentPane.add(textFieldCountTables);
		textFieldCountTables.setColumns(10);
		
		JLabel label_2 = new JLabel("Кол-во дней создания:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_2, 18, SpringLayout.SOUTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_2, 0, SpringLayout.WEST, label);
		contentPane.add(label_2);
		
		textFieldCountDays = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldCountDays, -3, SpringLayout.NORTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldCountDays, 0, SpringLayout.EAST, textFieldNumber);
		contentPane.add(textFieldCountDays);
		textFieldCountDays.setColumns(10);
		
		JLabel label_3 = new JLabel("Объем данных(Кб):");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_3, 19, SpringLayout.SOUTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_3, 0, SpringLayout.WEST, label);
		contentPane.add(label_3);
		
		textFieldDataCapacity = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldDataCapacity, -3, SpringLayout.NORTH, label_3);
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldDataCapacity, 0, SpringLayout.EAST, textFieldNumber);
		contentPane.add(textFieldDataCapacity);
		textFieldDataCapacity.setColumns(10);
		
		JLabel label_4 = new JLabel("Объем данных арх. док-тов(Кб):");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_4, 20, SpringLayout.SOUTH, label_3);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_4, 0, SpringLayout.WEST, label);
		contentPane.add(label_4);
		
		textFieldArchDataCapacity = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldArchDataCapacity, -3, SpringLayout.NORTH, label_4);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldArchDataCapacity, 0, SpringLayout.WEST, textFieldNumber);
		contentPane.add(textFieldArchDataCapacity);
		textFieldArchDataCapacity.setColumns(10);
		
		JLabel label_5 = new JLabel("Стоимость(тыс. б. р.):");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_5, 17, SpringLayout.SOUTH, label_4);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_5, 0, SpringLayout.WEST, label);
		contentPane.add(label_5);
		
		textFieldCost = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldCost, -3, SpringLayout.NORTH, label_5);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldCost, 0, SpringLayout.WEST, textFieldNumber);
		contentPane.add(textFieldCost);
		textFieldCost.setColumns(10);
		
		JButton buttonCancel = new JButton("Отмена");
		buttonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.SOUTH, buttonCancel, 0, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, buttonCancel, 0, SpringLayout.EAST, textFieldNumber);
		contentPane.add(buttonCancel);
		
		JButton buttonAdd = new JButton("Добавить");
		buttonAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Database database = addDatabase();
				if (database!=null) {
					if (currentServerWindow!=null)
						currentServerWindow.setNewModelForDatabase(database);
					if (currentWorkplaceWindow!=null)
						currentWorkplaceWindow.setNewModelForDatabase(database);
					JOptionPane.showMessageDialog(frame, "База данных добавлена.");
				}
				
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, buttonAdd, 0, SpringLayout.NORTH, buttonCancel);
		sl_contentPane.putConstraint(SpringLayout.EAST, buttonAdd, -6, SpringLayout.WEST, buttonCancel);
		contentPane.add(buttonAdd);
	}

	public static DatabaseWindow getFrame() {
		return frame;
	}

	public static void setFrame(DatabaseWindow frame) {
		DatabaseWindow.frame = frame;
	}
	
	public Database addDatabase() {
		Database database = new Database();
		String number = textFieldNumber.getText();
		if (Validation.isLengthBetween(number, 0, databaseNumberLength))
			database.setNumber(number);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неправильно задан номер. Номер может содержать не более "
							+ databaseNumberLength + " символов.");
			return null;
		}
		String countOfTables = textFieldCountTables.getText();
		if (Validation.isNumber(countOfTables))
			database.setCountOfTables(Integer.valueOf(countOfTables));
		else {
			JOptionPane.showMessageDialog(frame,
					"Указано неверное количество таблиц.");
			return null;
		}
		String countOfCreationDays = textFieldCountDays.getText();
		if (Validation.isNumber(countOfCreationDays))
			database.setCountOfCreationDays(Integer.valueOf(countOfCreationDays));
		else {
			JOptionPane.showMessageDialog(frame, "Указано неверное количество дней.");
			return null;
		}
		String dataCapacity = textFieldDataCapacity.getText();
		if (Validation.isNumber(dataCapacity))
			database.setDataCapacity(Integer.valueOf(dataCapacity));
		else {
			JOptionPane.showMessageDialog(frame, "Указано неверный объем.");
			return null;
		}
		String archieveDataCapacity = textFieldArchDataCapacity.getText();
		if (Validation.isNumber(archieveDataCapacity))
			database.setArchieveDataCapacity(Integer.valueOf(archieveDataCapacity));
		else {
			JOptionPane.showMessageDialog(frame, "Указан неверный объем архивных документов.");
			return null;
		}
		String cost = textFieldCost.getText();
		if (Validation.isNumber(cost))
			database.setCost(Integer.valueOf(cost));
		else {
			JOptionPane.showMessageDialog(frame, "Указана неверная цена.");
			return null;
		}
		database.setType(DatabaseType.Common);
		return database;
	}

	public ServerWindow getCurrentServerWindow() {
		return currentServerWindow;
	}

	public void setCurrentServerWindow(ServerWindow currentServerWindow) {
		this.currentServerWindow = currentServerWindow;
	}

	public WorkplaceWindow getCurrentWorkplaceWindow() {
		return currentWorkplaceWindow;
	}

	public void setCurrentWorkplaceWindow(WorkplaceWindow currentWorkplaceWindow) {
		this.currentWorkplaceWindow = currentWorkplaceWindow;
	}	
	
}
